package com.mbbmap.util;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.mbbmap.app.home.ParamHome;
import com.mbbmap.app.manager.ApplicationLogManager;
import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;
import com.mbbmap.app.manager.SystemLogManager;


/**
 * <code>GlobalInit</code> is a <code>Servlet</code> that is loaded upon startup and is used to
 * initialize any global variables or set application attributes.
 */
public class GlobalInit extends HttpServlet {

    private ConfigManager configMgr;
    private DBConnectionManager dbMgr;
    private ApplicationLogManager appLogMgr;
    private SystemLogManager sysLogMgr;
    ServletContext sc;

    public void init(ServletConfig conf) throws ServletException {
        super.init(conf);
        sc = conf.getServletContext();
        dbMgr = DBConnectionManager.getInstance();

        configMgr = ConfigManager.getInstance();
        //configMgr.verifyORMapping();

        sysLogMgr = SystemLogManager.getInstance();
        appLogMgr = ApplicationLogManager.getInstance();
        initOptions();

        // set the debugging flag into application context;
        System.out.println("<<<<< GlobalInit >>>>>>");
        System.out.println("..... Debug=" + configMgr.get(Constants.DEBUG));
        Boolean debug = Boolean.valueOf(configMgr.get(Constants.DEBUG));
        conf.getServletContext().setAttribute(Constants.DEBUG, debug);
    }

    private void initOptions() {
		Constants.globalParamProp = ParamHome.getInstance().getParamValues(Constants.APP_MERCHANT);
		// Store the parameter/config properties in session
		// sysadm/sysadm3456
    }

    public void destroy() {
        dbMgr.release();
        super.destroy();

    }
}